﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;
using AG.Utilities;


namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class ServiceCheckCXPACKET : IssueTypeCheckBase
    {

        public ServiceCheckCXPACKET()
        {
            IssueTypeId = 2018;
        }

        public override void Check()
        {
            try
            {
                // Database check AutoShrink is enabled 
                HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);


                // Query for Databases with auto shrink enabled

                var Qry = from s in dc.Services
                          from w in s.Waitstats
                          where w.WaitType == "CX_PACKET"
                          select s;
                         

                IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);

                foreach (var service in Qry)
                {

                    //Create new Issue

                    Issue issue = new Issue();
                    issue.IssueType = issueType;
                    issue.TargetId = service.ServiceID;
                    issue.Description = issueType.Description.Replace("{ServiceName}", service.Name);

                    dc.Issues.InsertOnSubmit(issue);
                }

                dc.SubmitChanges();

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

    }
}
